import { ref, onUnmounted } from 'vue';
const reConnectTimeout = ref(null);
export function useWebSocket() {
	const socketOpen = ref(false);
	const reSocketOpen = ref(false);
	const setIntervalName = ref(null);


	const connectWebSocket = () => {
		let url = 'wss://jtmckj.cn:9995/websocket';
		uni.connectSocket({
			url,
			success: () => {
				console.log('WebSocket连接成功');
			},
			fail: (res) => {
				console.log('WebSocket连接失败:', res);
				reConnect();
			}
		});

		uni.onSocketOpen(() => {
			console.log('WebSocket连接已打开');
			socketOpen.value = true;
			const globalData = uni.getStorageSync('globalData')
			// console.log(globalData,'globalData接收的参数')
			// 第一次链接 
			const shopInfo = {
				msgContent: {
					brandCode: "0005",
					consumeCode: "",
					cuttrentStateCode: "3",
					cuttrentStateName: "点单就餐",
					memberCode: "M00050000318",
					memberName: "微187****0108",
					orgCode: "0018",
					orgName: "上海空白区",
					roomCode: "0018018",
					roomName: "B07"
				},
				msgPlatform: 3,
				msgType: "heartbeat"
			};
			sendSocketMessage(shopInfo);

			setIntervalName.value = setInterval(() => {
				let modal = {
					msgContent: {
						consumeCode: "",
						memberCode: "M00050000318",
						memberName: "微187****0108",
						orderInfo: {
							attachment: "https://oss.jtmckj.com/backend/OrgGoods/2024101910380580歌手·阿村.jpg",
							flavor: "",
							goodsCode: "P000501",
							goodsName: "歌手·阿村",
							goodsType: "001818",
							groupCode: "001818",
							isEstmClear: "0",
							isJoinMinConsume: "1",
							isUseGold: "0",
							itemCode: "0018P000501041047",
							mathType: "plus",
							num: 2,
							orgCode: "0018",
							producePoint: "001803",
							saleCoins: "0",
							saleLimitMax: 999,
							saleLimitMin: 0,
							saleLimitStep: 1,
							salePrice: "200.00",
							saleQty: "13",
							state: "1",
							type: "1",
							uomCode: "041047",
							uomName: "首",
							vipPrice: "200.00"
						},
						roomCode: "0018018"
					},
					msgPlatform: 3,
					msgType: "message"
				}

				sendSocketMessage(modal);
			}, 5000);
		});

		uni.onSocketError((res) => {
			console.log('WebSocket连接打开失败:', res);
			reConnect();
		});

		uni.onSocketClose(() => {
			console.log('WebSocket连接已关闭');
			// reSocketOpen.value = false
			// reConnect();
		});

		uni.onSocketMessage((message) => {
			console.log('收到WebSocket消息:', message);
			try {
				const data = JSON.parse(message.data);

				handleMessage(data);

			} catch (error) {
				console.error('处理WebSocket消息时出错:', error);
			}
		});
	};

	const initSocketMessage = () => {
		// 监听消息更新
		uni.onSocketMessage((message) => {
			console.log('sendSocketMessage2：', message)
		});
	}


	const sendSocketMessage = (message) => {
		console.log('sendSocketMessage：', message)
		uni.sendSocketMessage({
			data: JSON.stringify(message),
			success: (e) => {},
			fail: (err) => {
				console.log(err);
				reConnect();
			}
		});
	};

	const reConnect = () => {
		// if (reSocketOpen.value) return;
		// clearTimeout(reConnectTimeout.value);
		//  reConnectTimeout.value = setTimeout(() => {
		//    reSocketOpen.value = true;
		// console.log('重连看看22222222222222')
		//    connectWebSocket();
		//  }, 15000);

		setTimeout(() => {
			console.log('重连')
			connectWebSocket();
		}, 10000) 

	};

	const handleMessage = (data) => {

	};
	
	// 关闭
	const closeConnectWebSocket = (data) => {
	    clearInterval(setIntervalName.value);
		uni.onSocketClose(() => { 
		});
	}; 

	onUnmounted(() => {
		clearInterval(setIntervalName.value);
		// clearTimeout(reConnectTimeout.value);
	});

	return {
		initSocketMessage,
		connectWebSocket,
		sendSocketMessage,
		socketOpen,
		closeConnectWebSocket
	};
}